import tornado.ioloop
import tornado.web


class IndexHandler(tornado.web.RequestHandler):
    def _initialize(self) -> None:
        print('_initialize')

    def initialize(self) -> None:
        print('initialize')

    def get(self, *args):
        print('get')
        self.write('Hello, world')
        self.send_error(404)

    def set_default_headers(self) -> None:
        print('set_default_headers')

    def on_finish(self) -> None:
        # 中间件1
        print('on_finish')

    def prepare(self):
        # 中间件2
        print('prepare')

    def post(self, *args):
        print('post')

    def write_error(self, status_code: int, **kwargs: tornado.ioloop.Any) -> None:
        super().write_error(status_code, **kwargs)
        print('write_error')

    def on_connection_close(self) -> None:
        print('on_connection_close')


def make_app():
    return tornado.web.Application([
        (r'/', IndexHandler),
    ], debug=True)


if __name__ == '__main__':
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()
